import type { RouteRecordRaw } from 'vue-router'

import { isOpenCache } from '@/config'
import routes from '@/router/routes'

/** 页面 缓存 hooks */
export function useCache() {
  const _cacheList = ref<Set<string>>(new Set())

  const openCache = isOpenCache
  const cacheList = computed(() => Array.from(_cacheList.value))

  const getCacheList = (source: RouteRecordRaw[]) => {
    source.forEach(item => {
      if ((item.children ?? []).length > 0) {
        getCacheList(item?.children ?? [])
      } else if (!item.meta?.ignoreKeepAlive && item.name) {
        _cacheList.value.add(item.name as string)
      }
    })
  }

  getCacheList(routes)
  return { cacheList, openCache }
}
